package controllers;

import models.Instructor;
import models.Student;

public class Security extends Secure.Security {
	public static Student getStudent() {
		Object user = getConnected();
		return user instanceof Student ? (Student) user : ((Instructor) user).studentAccount;
	}

	public static Object getConnected() {
		Object user = Student.find("byUsername", Security.connected()).first();
		if (user == null) {
			user = Instructor.find("byUsername", Security.connected()).first();
		}
		return user;
	}

	public static boolean authentify(String username, String password) {
		return Student.connect(username, password) || Instructor.connect(username, password);
	}

	public static boolean check(String profile) {
		if (profile == null) {
			profile = "";
		}

		Object user = getConnected();

		if (user == null) {
			return false;
		}

		// if (profile.equals("student")) {
		// // both instructors and students have student accounts now
		// // Use Security.getStudent()
		// return true;
		// } else
		if (profile.equals("ta")) {
			return user instanceof Instructor;
		} else if (profile.equals("admin")) {
			return user instanceof Instructor && ((Instructor) user).admin;
		}

		return false;
	}
}